package com.yunkeji.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yunkeji.common.annotation.Log;
import com.yunkeji.common.annotation.RepeatSubmit;
import com.yunkeji.common.core.controller.BaseController;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.domain.R;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.core.validate.AddGroup;
import com.yunkeji.common.core.validate.EditGroup;
import com.yunkeji.common.enums.BusinessType;
import com.yunkeji.common.utils.poi.ExcelUtil;
import com.yunkeji.dao.vo.UserSignVo;
import com.yunkeji.system.domain.bo.UserSignBo;
import com.yunkeji.system.service.IUserSignService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 用户签到
 *
 * @author zhang
 * @date 2024-07-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/userSign")
public class UserSignController extends BaseController {

    private final IUserSignService iUserSignService;

    /** 查询用户签到列表 */
    @SaCheckPermission("system:userSign:list")
    @GetMapping("/list")
    public TableDataInfo<UserSignVo> list(UserSignBo bo, PageQuery pageQuery) {
        return iUserSignService.queryPageList(bo, pageQuery);
    }

    /** 导出用户签到列表 */
    @SaCheckPermission("system:userSign:export")
    @Log(title = "用户签到", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UserSignBo bo, HttpServletResponse response) {
        List<UserSignVo> list = iUserSignService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户签到", UserSignVo.class, response);
    }

    /**
     * 获取用户签到详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:userSign:query")
    @GetMapping("/{id}")
    public R<UserSignVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iUserSignService.queryById(id));
    }

    /** 新增用户签到 */
    @SaCheckPermission("system:userSign:add")
    @Log(title = "用户签到", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody UserSignBo bo) {
        return toAjax(iUserSignService.insertByBo(bo));
    }

    /** 修改用户签到 */
    @SaCheckPermission("system:userSign:edit")
    @Log(title = "用户签到", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserSignBo bo) {
        return toAjax(iUserSignService.updateByBo(bo));
    }

    /**
     * 删除用户签到
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:userSign:remove")
    @Log(title = "用户签到", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iUserSignService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
